#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <unistd.h>

void *open_mmap(const char *filename);
void close_mmap(void *m);

int main(int argc, char **argv)
{
	void *m = open_mmap("test.txt");
	if ( m == NULL ) {
		return -1;
	}
	printf("pid=%d\n", getpid());
	printf("0x%llx:%s\n", (long long)m, (char *)m);
	sleep(60);
	return 0;
}

void *open_mmap(const char *filename)
{
	struct fb_var_screeninfo vinfo;
	void *m = NULL;
	int fd = open(filename, O_RDWR);
	if (fd == -1) {
		perror(filename);
		return NULL;
	}
//	if ( ioctl(fd, FBIOGET_VSCREENINFO, &vinfo) == -1 ) {
//		perror(filename);
//		close(fd);
//		return NULL;
//	}
	m = mmap(NULL, 2*1024*1024, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
	if (m == NULL) {
		perror(filename);
		close(fd);
	}
	close(fd);
	return m;
}

void close_mmap(void *m)
{
	if ( m == NULL ) {
		return;
	}
	munmap(m, 2*1024*1024);
}
